package com.xutong.myfunction.custom;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xutong.myfunction.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hasee on 2016/8/18.
 */
public class CustomLunBo {

    private ViewPager vpMainAdvert;
    private List<ImageView> imageList;//轮播图
    private LinearLayout llAdvertBtn;//小圆点容器
    private List<Button> btnList;//小圆点
    private final int SING = 99;//messgae传递标志
    private int index = 0;//广告viewpager下标
    private Timer timer = null;
    private MyTimerTask myTimerTask = null;

    private Context mContext;

    public CustomLunBo (Context context, ViewPager vpMainAdvert,LinearLayout llAdvertBtn){
        this.mContext = context;
        this.vpMainAdvert = vpMainAdvert;
        this.llAdvertBtn = llAdvertBtn;

        initViewPager();

    }



    private void initViewPager() {

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15,15);
        layoutParams.setMargins(10,0,10,0);

        imageList = new ArrayList<>();
        btnList = new ArrayList<>();
        for (int i=0;i<4;i++){
            ImageView image1 = new ImageView(mContext);
            image1.setScaleType(ImageView.ScaleType.FIT_XY);
            image1.setImageResource(R.drawable.jl_main_banner);
            imageList.add(image1);
            Button btn1 = new Button(mContext);
            btn1.setBackgroundResource(R.drawable.viewpager_button_white);
            btn1.setLayoutParams(layoutParams);
            llAdvertBtn.addView(btn1);
            btnList.add(btn1);
        }

        AdvertViewPager advertViewPager = new AdvertViewPager(imageList);
        vpMainAdvert.setAdapter(advertViewPager);
        vpMainAdvert.setOnPageChangeListener(new AdvertPageChangeListener());

    }

    class AdvertViewPager extends PagerAdapter {

        private List<ImageView> mListViews;

        public AdvertViewPager(List<ImageView> mListViews) {
            this.mListViews = mListViews;//构造方法，参数是我们的页卡，这样比较方便。
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListViews.get(position));
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mListViews.get(position), 0);
            return mListViews.get(position);
        }

    }

    class AdvertPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            initButton(position);
//            System.out.println("index="+index+",position="+position);
            index = position;

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }


    private void initButton(int index) {
        for (int i=0;i<btnList.size();i++){
            if (i==index){
                btnList.get(i).setBackgroundResource(R.drawable.viewpager_button_red);
            }else {
                btnList.get(i).setBackgroundResource(R.drawable.viewpager_button_white);
            }
        }

    }

    public void start(){
        timer = new Timer();
        myTimerTask = new MyTimerTask();
        timer.schedule(myTimerTask, 5000, 5000);
    }

    public void stop(){
        if (timer != null) {
            if (myTimerTask != null) {
                myTimerTask.cancel();
            }
            timer.purge();

        }
    }


    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == SING) {
                index++;
                if (imageList != null) {
                    if (index > (imageList.size() - 1)) {
                        index = 0;
                        vpMainAdvert.setCurrentItem(index);
                    } else {
                        vpMainAdvert.setCurrentItem(index);
                    }
                }

            }

        }
    };

    class MyTimerTask extends TimerTask {

        @Override
        public void run() {

            Message msg = new Message();
            msg.what = SING;
            handler.sendMessage(msg);
        }
    }


}
